var mapContainer = new Ext.Map();
var markerFrom;
var markerTo;
var markerToDrop;
var direction;
var activeMenu;
var markerCurrent;
var currentMode;

Wheels.TripPlannerMap = new Ext.Panel({
    layout: 'fit',
    items: [mapContainer]
});

var departurePointButton = { xtype: 'button', text: 'Set Departure Point',
    handler: function () {
        if (markerFrom) {
            markerFrom.setMap(null);
            markerFrom = null;
        }

        markerFrom = markerToDrop;
        markerToDrop = null;
        activeMenu.hide();
        getAddressByLatLng(markerFrom.position, function (address) { updateDeparture(address) });
        setCard('trip-planner');
    }
}

var arrivalPointButton = { xtype: 'button', text: 'Set Arrival Point',
    handler: function () {
        if (markerTo) {
            markerTo.setMap(null);
            markerTo = null;
        }

        markerTo = markerToDrop;
        markerToDrop = null;
        activeMenu.hide();
        getAddressByLatLng(markerTo.position, function (address) { updateArrival(address) });
        setCard('trip-planner');
    }
}

var departurePoint = new Ext.Panel({
    floating: true,
    centered: true,
    width: 260,
    scroll: 'vertical',
    items: [departurePointButton, { xtype: 'button', text: 'Cancel', handler: hideActiveMenu}]
});

var arrivalPoint = new Ext.Panel({
    floating: true,
    centered: true,
    width: 260,
    scroll: 'vertical',
    items: [arrivalPointButton, { xtype: 'button', text: 'Cancel', handler: hideActiveMenu}]
});

function showActiveMenu() {
    activeMenu.setCentered(true);
    activeMenu.show();
    activeMenu.setPosition((document.body.clientWidth / 2) - 130, (document.body.clientHeight / 2) + 30);
}

function hideActiveMenu() {
    activeMenu.hide();

    if (markerToDrop)
        markerToDrop.setMap(null);
}

function dropPin(location) {
    if (currentMode != 'trip-planning')
        return;

    if (markerToDrop)
        markerToDrop.setMap(null);

    markerToDrop = new google.maps.Marker({ position: location, map: mapContainer.map });
    mapContainer.map.panTo(location);
    setTimeout(function () { showActiveMenu() }, 300);
}

function updateMarkerFrom(location) {
    if (markerToDrop)
        markerToDrop.setMap(null);

    activeMenu = departurePoint;

    if (!location) {
        mapContainer.map.setZoom(5);
        return;
    }

    if (!markerFrom)
        markerToDrop = new google.maps.Marker({ position: location, map: mapContainer.map });

    mapContainer.map.panTo(location);
    mapContainer.map.setZoom(17);

    if (!markerFrom)
        setTimeout(function () { showActiveMenu() }, 300);

    //setTimeout(function () { showStopsForBounds() }, 300);
}

function updateMarkerTo(location) {
    if (markerToDrop)
        markerToDrop.setMap(null);

    activeMenu = arrivalPoint;

    if (!location) {
        mapContainer.map.setZoom(5);
        return;
    }

    if (!markerTo)
        markerToDrop = new google.maps.Marker({ position: location, map: mapContainer.map });

    mapContainer.map.panTo(location);
    mapContainer.map.setZoom(17);

    if (!markerTo)
        setTimeout(function () { showActiveMenu() }, 300);

    //setTimeout(function () { showStopsForBounds() }, 300);
}

function isGeoLocationSupported() {
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/Android/i))) {
        if (navigator.geolocation) { return true; }
        else { alert("No GPS Functionality."); }
    } else { alert('No IPhone, IPad, Android'); }

    return false;
}

function setCurrentLocation(direction) {
    if (isGeoLocationSupported()) {
        navigator.geolocation.getCurrentPosition(function (position) { setCurrentGPS(position, direction); }, gpsError);
    } else {
        setCurrentGPS(new google.maps.LatLng(38.9156487, -77.0737149), direction);
    }
}

function showCurrentLocation() {
    if (isGeoLocationSupported()) {
        navigator.geolocation.getCurrentPosition(showGPS, gpsError);
    }
}

function gpsError(error) {
    alert("GPS Error: " + error.code + ", " + error.message);
}

function setCurrentGPS(position, direction) {
    switch (direction) {
        case "departure":
            getAddressByLatLng(position, function (address) { updateDeparture(address) });
            break;
        case "arrival":
            getAddressByLatLng(position, function (address) { updateArrival(address) });
            break;
    }
}

function showGPS(position) {
    //    var locaton = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    //    markerCurrent = new google.maps.Marker({ position: location, map: mapContainer.map });
    //    mapContainer.map.panTo(location);
    //    mapContainer.map.setZoom(17);
    alert("Current position: Lat: " + position.coords.latitude + "; Lon: " + position.coords.longitude);
}



